import on, { IHandle } from '@feidao-factory/core/on';
import { IEvents } from '../interfaces';

export default function init_events(events: IEvents, emit: (event: string, ...args: any[]) => Promise<any> | undefined) {
	return Object.keys(events).map((event) => {
		return on(window, event, (e) => {
			return emit(event, e);
		}, true) as IHandle;
	});
	// const handles = [] as IHandle[];
	// for (const event in events) {
	// 	if (event === FD_EVENTS_INIT) {
	// 		continue;
	// 	}
	// 	if (is_win_event(event)) {
	// 		const handle = on(window, event, (e) => {
	// 			return emit(event, e);
	// 		}, true);
	// 		handles.push(handle);
	// 	} else {
	// 		throw new Error(`not supported event [${event}]`);
	// 	}
	// }
	// todo: Do we need listen to a message here?
	// ipcRenderer.on('msg-pipe', async (_e: unknown, { action, msg, reply }: IDesktopMsg) => {
	// 	try {
	// 		const data = await emit(action, msg);
	// 		ipcRenderer.send(reply, {
	// 			data
	// 		});
	// 	} catch (ee) {
	// 		const err = ee as Error;
	// 		ipcRenderer.send(reply, {
	// 			errMsg: err.message
	// 		});
	// 	}
	// });
	// return handles;
}
